package com.pet.platform.controller;

import com.pet.platform.common.PageResult;
import com.pet.platform.common.Result;
import com.pet.platform.dto.ProductDTO;
import com.pet.platform.entity.Product;
import com.pet.platform.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/{id}")
    public Result<Product> findById(@PathVariable Long id) {
        return Result.success(productService.findById(id));
    }

    @GetMapping
    public Result<PageResult<ProductDTO>> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String category) {
        return Result.success(productService.findPage(pageNum, pageSize, keyword, category));
    }

    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> create(@Valid @RequestBody ProductDTO productDTO) {
        productService.create(productDTO);
        return Result.success();
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> update(@PathVariable Long id, @Valid @RequestBody ProductDTO productDTO) {
        productService.update(id, productDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> delete(@PathVariable Long id) {
        productService.delete(id);
        return Result.success();
    }

    @PostMapping("/{id}/image")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> uploadImage(@PathVariable Long id, @RequestParam("file") MultipartFile file) {
        return Result.success(productService.uploadImage(id, file));
    }

    @PutMapping("/{id}/stock")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> updateStock(@PathVariable Long id, @RequestParam Integer stock) {
        productService.updateStock(id, stock);
        return Result.success();
    }
    
    @GetMapping("/categories")
    public Result<List<Map<String, String>>> getCategories() {
        // 返回预定义的商品分类列表
        List<Map<String, String>> categories = Arrays.asList(
            createCategory("food", "宠物食品"),
            createCategory("toys", "玩具用品"),
            createCategory("health", "保健品"),
            createCategory("daily", "日用品"),
            createCategory("cleaning", "清洁用品"),
            createCategory("other", "其他")
        );
        return Result.success(categories);
    }
    
    private Map<String, String> createCategory(String value, String label) {
        Map<String, String> category = new HashMap<>();
        category.put("value", value);
        category.put("label", label);
        return category;
    }
} 